package com.scs.application.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * @Description：缓存配置
 */
@Slf4j
@Configuration
@EnableCaching
public class CacheConfig {


    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();

        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("/cache/ehcache.xml"));
        /**
         * 濡傛灉shared 涓簍rue锛屽垯琛ㄧず spring浣跨敤鐨刢acheManager涓轰竴涓崟渚嬪璞★紝璇︽儏鏌ョ湅锛歿@link EhCacheManagerFactoryBean#afterPropertiesSet }
         * 如果shared 为true，则表示 spring使用的cacheManager为一个单例对象，详情查看：{@link EhCacheManagerFactoryBean#afterPropertiesSet }
         */
        cacheManagerFactoryBean.setShared(true);
        return cacheManagerFactoryBean;
    }

    @Bean
    public CacheManager ehcacheManager(EhCacheManagerFactoryBean factoryBean){
        return new EhCacheCacheManager(factoryBean.getObject());
    }
}

